package com.example.blog.controller;

import com.example.blog.pojo.Result;
import com.example.blog.utils.AliOssUtil;
import com.example.blog.utils.AliyunOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class FileUploadController {
    @Autowired
    private AliyunOssUtil aliyunOssUtil;
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file)throws Exception{
        //把文件的内容存储到本地磁盘上
        String originalfilename = file.getOriginalFilename();
        //保证文件的名字是唯一的,从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalfilename.substring(originalfilename.lastIndexOf("."));
        //file.transferTo(new File("C:\\Users\\Administrator\\Desktop\\files\\"+filename));
        String url = aliyunOssUtil.upload(filename,file.getInputStream());

        return Result.success(url);

    }
}
